<template>
  <el-dialog :title="upload.title" :visible.sync="upload.open" width="800px" append-to-body>
    <el-form ref="form" :model="form" :rules="rules" label-width="150px">
        <el-row>
          <el-col :span="20">
            <el-form-item label="名称" prop="name">
              <el-input v-model="form.name"  placeholder="请输入名称" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="编号" prop="serialNo">
              <el-input v-model="form.serialNo" placeholder="请输入编号" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="上级目录" prop="">
               <tree-select :options="optionsTree" v-model="form.parentId" :searchable="false"
                :default-expand-level="Infinity" :open-on-click="true" :open-on-focus="true"
                :normalizer="normalizer" placeholder="请选择上级目录" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="12">
            <el-form-item label="排序" prop="sort">
              <el-input-number v-model="form.sort" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="20">
            <el-form-item label="备注" prop="remark">
              <el-input v-model="form.remark" placeholder="请输入备注" maxlength="30" />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="upload.open = false">取 消</el-button>
      </div>
  </el-dialog>
</template>

<script>
import TreeSelect from '@riophae/vue-treeselect'
import '@riophae/vue-treeselect/dist/vue-treeselect.css'
import { listCategory, checkCategory, addCategory } from "@/api/material/category";
export default {
  name: "index", 
  components: {
    TreeSelect
  },
  data(){
    return {
      form: {},
      optionsTree:[],
      normalizer(node){
        if(node.children && !node.children.length){
          delete node.children
        }
        return {
          id: node.id,
          label: node.name,
          children: node.children,
        }
      },
      // 表单校验
      rules: {
        name: [
          { required: true, message: "名称不能为空", trigger: "blur" },
          { min: 2, max: 30, message: '长度在 2 到 30 个字符', trigger: 'blur' },
          { validator: this.validateAccountName}
        ],
        serialNo: [
          { required: true, message: "编号不能为空", trigger: "blur" }
        ]
      },
    }
  },
  props: {
    upload: {
        type: Object,
        default: "",
        require: true
    },
  },
  methods:{
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != undefined) {
            /**updateSupplier(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });*/
          } else {
             addCategory(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.upload.open = false;
              this.loadNodeTree();
            });
          }
        }
      });
    },
    loadNodeTree(){
      listCategory(0).then(response => {
        this.optionsTree = response.data
      })    
    },
    validateAccountName(rule, value, callback){
        let params = {
          name: value
        };
        checkCategory(params).then((res)=>{
          if(res && res.code===200) {
            if(!res.data){
              callback();
            } else {
              callback("名称已经存在");
            }
          } else {
            callback(res.data);
          }
        });
      }
  } 
}
</script>